package main;

import java.text.DecimalFormat;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.TimeUnit;

public class Report {
    private static final float NANO_TO_MILLIS = 1000000;
    private static final float NANO_TO_SEC = 1000000000;
	private String url;
	private int totalUrls;
	private int numWords;
	private int numPages;
	private Map<String,Integer> keyWordCount;
	private Map<String,Integer> currentkeyWordCount;
	private float parseTime;
	private String[] my_keyWordList;
	private float totalRunTime;



	public Report(String[] the_keyWordList) {
		super();
		this.url = "";
		this.totalUrls = 0;
		this.numWords = 0;
		this.keyWordCount = new HashMap<String,Integer>();
		this.currentkeyWordCount = new HashMap<String,Integer>();
		this.parseTime = 0;
		this.numPages = 0;
		this.my_keyWordList = the_keyWordList;
		this.totalRunTime = 0;
	
	}

	public String getUrl() {
		return url;
	}

	public void setUrl(String url) {
		this.url = url;
	}


	public void addTotalPages(int the_numPages) {
		this.numPages += the_numPages;
	}
	public void addTotalUrls(int totalUrls) {
		this.totalUrls += totalUrls;
	}


	public void setNumWords(int numWords) {
		this.numWords += numWords;
	}

	public Map<String, Integer> getKeyWordCount() {
		return keyWordCount;
	}
	public void setTotalTime(long the_totalTime){
		this.totalRunTime = the_totalTime;
		
	}
	public void setCurrentKeyWordCount(Map<String, Integer> the_map){
		this.currentkeyWordCount = the_map;
	}
	
	public void addKeyWordCount(Map<String, Integer> the_map) {
		int tempTotalCount = 0;
		for (int i = 0; i < my_keyWordList.length; i++) {
	
		  int tempCount = 0;
		    if (the_map.containsKey(my_keyWordList[i])) {
		    	tempCount = the_map.get(my_keyWordList[i]).intValue();
		    	if (keyWordCount.containsKey(my_keyWordList[i])){
		    		tempTotalCount = keyWordCount.get(my_keyWordList[i]).intValue();
		    		tempTotalCount += tempCount;
		      keyWordCount.put(my_keyWordList[i], tempTotalCount);
		    } else {
		          keyWordCount.put(my_keyWordList[i], tempCount);
		    }
		   }
		}
	}
	

	public void setParseTime(long d) {
		this.parseTime += d;
	}
	

	public void Print(){
	
		    
	        DecimalFormat mill = new DecimalFormat("##.####");
	        DecimalFormat sec = new DecimalFormat("###.###");
	        DecimalFormat ave = new DecimalFormat("#.###");
	       /* TimeUnit.NANOSECONDS.toSeconds(ttime);
			my_report.setTotalTime(TimeUnit.SECONDS.convert(ttime, TimeUnit.NANOSECONDS));
	        */
	        //double avewords = 
	        double var_tempCount = 0;
	        double var_tempCount1 = 0;
	        
			System.out.println("Parsed: "+ url);
			System.out.println("Pages Retrieved: "+ numPages);
			System.out.println("Average Words Per Page: " + numWords/numPages);
			System.out.println("Average URLs per page: " + (totalUrls/numPages));
			System.out.println("Keyword \t\t Ave. hits per page \t\t Total hits");
	 
		  for (int k = 0; k < my_keyWordList.length; k++){
		    	if (currentkeyWordCount.containsKey(my_keyWordList[k])){
		    	var_tempCount1 = currentkeyWordCount.get(my_keyWordList[k]).intValue();
		    	var_tempCount = keyWordCount.get(my_keyWordList[k]).intValue();
		    	double pcount = var_tempCount1/numPages;
		    	System.out.println(my_keyWordList[k] + "\t\t  " + ave.format(pcount) +"\t\t\t\t\t " + var_tempCount);
		    	//System.out.println(my_keyWordList[k] + "\t\t  " +  (var_tempCount1+ "  "+ var_tempCount ));
		    	}
		  }
		  
			System.out.println("Page Limit: 5000");
			System.out.println("Average parse time per page: " + mill.format((parseTime/numPages)/NANO_TO_MILLIS) + " mills");
			System.out.println("Total running time: "+ sec.format(totalRunTime/NANO_TO_SEC) + " sec");
			System.out.println("+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++\n");
		
	
		  }
}

